package com.fowo.api.syncAmazon.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.incrementer.DefaultIdentifierGenerator;
import com.baomidou.mybatisplus.core.incrementer.IdentifierGenerator;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fowo.api.common.model.RException;
import com.fowo.api.entity.FbaShipment;
import com.fowo.api.entity.FbaShipmentProduct;
import com.fowo.api.entity.TmpFbaShipmentProduct;
import com.fowo.api.mapper.FbaShipmentMapper;
import com.fowo.api.mapper.FbaShipmentProductMapper;
import com.fowo.api.mapper.TmpFbaShipmentProductMapper;
import com.fowo.api.syncAmazon.mapper.ShipmentMapper;
import com.google.common.collect.Lists;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
@Slf4j
public class TmpFbaShipmentProductAmazonServiceImpl extends ServiceImpl<TmpFbaShipmentProductMapper, TmpFbaShipmentProduct> {

    @Resource
    private FbaShipmentMapper fbaShipmentMapper;
    @Resource
    private FbaShipmentProductMapper shipmentProductMapper;
    @Resource
    private ShipmentMapper shipmentMapper;

    private final IdentifierGenerator identifierGenerator = new DefaultIdentifierGenerator();

    @Transactional(rollbackFor = Exception.class)
    public void saveItem(List<TmpFbaShipmentProduct> products) {
        if (products == null || products.size() < 1) {
            throw new RException("Fba货架商品为空");
        }
        QueryWrapper queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("is_synchronous", "1");
        List<FbaShipment> shipments = fbaShipmentMapper.selectList(queryWrapper);
        Map<String, List<FbaShipment>> collect = null;
        if (shipments != null && shipments.size() > 0) {
            collect = shipments.stream().collect(Collectors.groupingBy(FbaShipment::getShipmentId));
        }
        //查询当前库中所有亚马逊数据 判断是否存在
        List<String> allSku = shipmentMapper.getAllSku();
        List<List<TmpFbaShipmentProduct>> partition = Lists.partition(products, 200);
        List<FbaShipmentProduct> list = new ArrayList<>();
        List<FbaShipmentProduct> updateList = new ArrayList<>();
        for (List<TmpFbaShipmentProduct> productList : partition) {
            if (this.saveBatch(productList)) {
                for (TmpFbaShipmentProduct product : productList) {
                    FbaShipmentProduct shipmentProduct = new FbaShipmentProduct();
                    shipmentProduct.setId((Long) identifierGenerator.nextId(null));
                    shipmentProduct.setIsSynchronous("1");
                    shipmentProduct.setCreateTime(new Date());
                    shipmentProduct.setDelFlag(false);
                    shipmentProduct.setMsku(product.getSellerSku());
                    shipmentProduct.setFnsku(product.getFulfillmentNetworkSku());
                    shipmentProduct.setAmountDeclared(product.getQuantityShipped());
                    shipmentProduct.setQuantityReceived(product.getQuantityReceived());
                    if(collect != null){
                        List<FbaShipment> shipments1 = collect.get(product.getShipmentId());
                        //当前货件商品未找到对应的父级关系 略过
                        if (shipments1 == null) {
                            continue;
                        }
                        shipmentProduct.setParentFbaShipment(shipments1.get(0).getId());
                    }
                    if(allSku.contains(product.getSellerSku())){
                        shipmentProduct.setLastUpdateTime(new Date());
                        updateList.add(shipmentProduct);
                    }else {
                        shipmentProduct.setCreateTime(new Date());
                        list.add(shipmentProduct);
                    }
                }
                if(list != null && list.size() > 0){
                    shipmentMapper.insertShipmentProduct(list);
                }
                if(updateList != null && updateList.size() > 0){
                    shipmentMapper.updateShipmentProduct(updateList);
                }
            }
        }
    }
}
